#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Git仓库迁移工具测试脚本
用于验证基本功能是否正常
"""

import os
import json
import tempfile
from git_migrator import GitMigrator


def test_config_validation():
    """测试配置验证功能"""
    print("=== 测试配置验证功能 ===")
    
    # 创建测试配置
    test_config = {
        "source_repo": "https://github.com/test",
        "source_username": "testuser",
        "source_password": "testpass",
        "target_gitlab_url": "https://gitlab.com",
        "target_access_token": "test_token",
        "target_project_path": "test/group",
        "local_temp_path": "./test_temp",
        "source_project_paths": ["project1", "project2"]
    }
    
    # 创建临时配置文件
    with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False, encoding='utf-8') as f:
        json.dump(test_config, f)
        temp_config_path = f.name
    
    try:
        # 测试配置加载
        migrator = GitMigrator(temp_config_path)
        print("✓ 配置文件加载成功")
        
        # 测试配置验证
        if migrator.validate_config():
            print("✓ 配置验证通过")
        else:
            print("✗ 配置验证失败")
            
        # 测试配置保存
        migrator.save_config(test_config)
        print("✓ 配置保存功能正常")
        
    finally:
        # 清理临时文件
        os.unlink(temp_config_path)
        if os.path.exists("./test_temp"):
            import shutil
            shutil.rmtree("./test_temp")
    
    print()


def test_missing_config():
    """测试缺失配置的情况"""
    print("=== 测试缺失配置处理 ===")
    
    # 测试不存在的配置文件
    migrator = GitMigrator("nonexistent.json")
    if not migrator.config:
        print("✓ 正确处理不存在的配置文件")
    else:
        print("✗ 未正确处理不存在的配置文件")
    
    print()


def test_invalid_config():
    """测试无效配置的情况"""
    print("=== 测试无效配置处理 ===")
    
    # 创建无效的JSON文件
    with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False, encoding='utf-8') as f:
        f.write("{ invalid json content")
        temp_config_path = f.name
    
    try:
        migrator = GitMigrator(temp_config_path)
        if not migrator.config:
            print("✓ 正确处理无效的JSON配置")
        else:
            print("✗ 未正确处理无效的JSON配置")
    finally:
        os.unlink(temp_config_path)
    
    print()


def test_logging_setup():
    """测试日志设置"""
    print("=== 测试日志设置 ===")
    
    # 创建临时配置文件
    test_config = {
        "source_repo": "https://github.com/test",
        "source_username": "testuser",
        "source_password": "testpass",
        "target_gitlab_url": "https://gitlab.com",
        "target_access_token": "test_token",
        "target_project_path": "test/group",
        "local_temp_path": "./test_temp",
        "source_project_paths": ["project1"]
    }
    
    with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False, encoding='utf-8') as f:
        json.dump(test_config, f)
        temp_config_path = f.name
    
    try:
        migrator = GitMigrator(temp_config_path)
        
        # 测试日志记录
        migrator.logger.info("测试日志信息")
        migrator.logger.warning("测试警告信息")
        
        # 检查日志文件是否创建
        if os.path.exists("migration.log"):
            print("✓ 日志文件创建成功")
            
            # 读取日志内容
            with open("migration.log", 'r', encoding='utf-8') as f:
                log_content = f.read()
                if "测试日志信息" in log_content and "测试警告信息" in log_content:
                    print("✓ 日志记录功能正常")
                else:
                    print("✗ 日志记录功能异常")
        else:
            print("✗ 日志文件未创建")
            
    finally:
        os.unlink(temp_config_path)
        if os.path.exists("migration.log"):
            os.unlink("migration.log")
        if os.path.exists("./test_temp"):
            import shutil
            shutil.rmtree("./test_temp")
    
    print()


def main():
    """主测试函数"""
    print("开始测试Git仓库迁移工具...\n")
    
    try:
        test_config_validation()
        test_missing_config()
        test_invalid_config()
        test_logging_setup()
        
        print("🎉 所有测试完成！")
        print("\n注意：这些测试仅验证基本功能，不包含实际的Git操作测试。")
        print("要测试完整的迁移功能，请配置真实的仓库信息并运行迁移。")
        
    except Exception as e:
        print(f"测试过程中发生错误: {e}")
        import traceback
        traceback.print_exc()


if __name__ == "__main__":
    main()

